<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Associations extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/users
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index()
	{ 	
     	$data = array('title' => 'FAT Application');
       	$this->load->view('default/header', $data);
		$this->load->view('default/associations');
		$this->load->view('default/footer');
	}
	
	public function get_all()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$this->load->model('Associations_model', 'associations_model');
     	$associationsObjectsArray = $this->associations_model->get_all();
     	$associationsJSON= json_encode($associationsObjectsArray);
     	echo $associationsJSON; 
	}
	
	public function save_association()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
	
		$raison_social = $this->input->post('raison_social');
     	$sigle = $this->input->post('sigle');
     	$adresse = $this->input->post('adresse');
     	$telephone = $this->input->post('telephone');
     	$mail = $this->input->post('mail');
     	$fondateur = $this->input->post('fondateur');
     	$date_fondation = $this->input->post('date_fondation');
     	$contact = $this->input->post('contact');
     	$description = $this->input->post('description');

     	if($raison_social && $sigle && $adresse && $telephone && $mail && $fondateur && $date_fondation && $contact && $description){
           	$this->load->model('Associations_model', 'associations_model');
           	
           	$record["raison_social"] = $raison_social;
	     	$record["sigle"] = $sigle;
	     	$record["adresse"] = $adresse;
	     	$record["telephone"] = $telephone;
	     	$record["mail"] = $mail;
	     	$record["fondateur"] = $fondateur;
	     	$record["date_fondation"] = $date_fondation;
	     	$record["contact"] = $contact;
	     	$record["description"] = $description;
           	
           	$result = $this->associations_model->insert($record);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occured.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
	public function update_association($id)
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$raison_social = $this->input->post('raison_social');
     	$sigle = $this->input->post('sigle');
     	$adresse = $this->input->post('adresse');
     	$telephone = $this->input->post('telephone');
     	$mail = $this->input->post('mail');
     	$fondateur = $this->input->post('fondateur');
     	$date_fondation = $this->input->post('date_fondation');
     	$contact = $this->input->post('contact');
     	$description = $this->input->post('description');

     	if($raison_social && $sigle && $adresse && $telephone && $mail && $fondateur && $date_fondation && $contact && $description){
           	$this->load->model('Associations_model', 'associations_model');
           	
           	$record["raison_social"] = $raison_social;
	     	$record["sigle"] = $sigle;
	     	$record["adresse"] = $adresse;
	     	$record["telephone"] = $telephone;
	     	$record["mail"] = $mail;
	     	$record["fondateur"] = $fondateur;
	     	$record["date_fondation"] = $date_fondation;
	     	$record["contact"] = $contact;
	     	$record["description"] = $description;
           	
           	$result = $this->associations_model->update($id, $record);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occurred.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
	public function remove_association()
	{
		$this->load->model('Users_model', 'user');
      	if( !$this->user->loggedin() )	redirect('/login', 'refresh');
      	
     	$id = $this->input->post('id');
     	if($id){
           	$this->load->model('Associations_model', 'associations_model');
           	$result = $this->associations_model->remove($id);
           	if ($result){
              	echo json_encode(array('success'=>true));
              } else {
              	echo json_encode(array('msg'=>'Some errors occurred.'));
              }
     	}else
           	echo json_encode(array('msg'=>'No needed data.'));
	}
	
}

/* End of file welcome.php */
/* Location: ./application/controllers/users.php */